package com.zhipu.balance.utils.print.dami

import android.text.TextUtils
import android.view.View
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.mobile.event.RecordDaMi
import com.zhipu.balance.mobile.event.RecordRice
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.print.BrightekCommandM
import java.nio.charset.Charset

/**
 * 大米打印
 */
object DaMiPrintUtils {

    /**
     * 大米打印
     */
    fun DaMiPrinting(mRecordDaMi : RecordDaMi){
        val stringBuilder = StringBuilder()
        val title = "大米计算"
        val retract = ((1.0 - title.length / 16.0) * 16).toInt()
        for (i in 0 until retract) {
            stringBuilder.append(" ")
        }
        stringBuilder.append(title).append('\r')
        //编号
        stringBuilder.append("编号：").append(mRecordDaMi.uniqueId).append('\r')
        //时间
        stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordDaMi.inspectionDate)).append('\r')
        /**
         * 杂质记录员
         */
        if(!TextUtils.isEmpty(mRecordDaMi.impurityRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("杂质").append('\r')
            stringBuilder.append("\r")
            // 大米-试样质量
            stringBuilder.append("试样质量(G)：").append(mRecordDaMi.riceSampleQuality).append('\r');
            //大米-杂质
            stringBuilder.append("大米杂质(G)：").append(mRecordDaMi.riceImpurity).append('\r');
            //杂质率
            stringBuilder.append("杂质率(%)：").append(mRecordDaMi.impurityResults).append('\r');
            //杂质记录员
            stringBuilder.append("杂质记录员：").append(mRecordDaMi.impurityRecorder).append('\r');
        }
        //不完善粒-试样质量
        if(!TextUtils.isEmpty(mRecordDaMi.imperfectGrainRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("不完善粒").append('\r');
            stringBuilder.append("\r");
            // 不完善粒-试样质量
            stringBuilder.append("试样质量(G)：").append(mRecordDaMi.qualityOfImperfectGrainSample).append('\r');
            /**
             * 细分
             */
            if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){
                //未熟粒
                stringBuilder.append("未熟粒(G)：").append(mRecordDaMi.immatureBrokenGrainQuality).append('\r')
                //未熟粒率
                stringBuilder.append("未熟粒率(%)：").append(mRecordDaMi.immatureGrainResult).append('\r')
                //虫蚀粒
                stringBuilder.append("虫蚀粒(G)：").append(mRecordDaMi.insectBoredQuality).append('\r')
                //虫蚀率率
                stringBuilder.append("虫蚀率率(%)：").append(mRecordDaMi.insectBoredResult).append('\r')
                //病斑粒
                stringBuilder.append("病斑粒(G)：").append(mRecordDaMi.mottledQuality).append('\r')
                //病斑粒率
                stringBuilder.append("病斑粒率(%)：").append(mRecordDaMi.mottledResult).append('\r')
                //生霉粒
                stringBuilder.append("生霉粒(G)：").append(mRecordDaMi.moldWeight).append('\r')
                //生霉粒率
                stringBuilder.append("生霉粒率(%)：").append(mRecordDaMi.moldResult).append('\r')
                //糙米粒
                stringBuilder.append("糙米粒(G)：").append(mRecordDaMi.incompleteBrownRiceQuality).append('\r')
                //糙米粒率
                stringBuilder.append("糙米粒率(%)：").append(mRecordDaMi.rougheningResults).append('\r')

            }else{
                //不完善粒质量
                stringBuilder.append("不完善粒质量(G)：").append(mRecordDaMi.imperfectGrainQuality).append('\r');
            }

            //不完善粒-结果
            stringBuilder.append("不完善粒率(%)：").append(mRecordDaMi.resultOfImperfectGrain).append('\r');
            //出糙率记录员
            stringBuilder.append("不完善粒记录员：").append(mRecordDaMi.imperfectGrainRecorder).append('\r');
        }
        //小碎米记录员
        if(!TextUtils.isEmpty(mRecordDaMi.brokenRiceRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("碎米").append('\r');
            stringBuilder.append("\r");
            //碎米-试样质量
            stringBuilder.append("碎米质量(G)：").append(mRecordDaMi.qualityOfBrokenRiceSample).append('\r');
            //碎米质量
            stringBuilder.append("碎米质量(G)：").append(mRecordDaMi.brokenRiceQuality).append('\r');
            //碎米率
            stringBuilder.append("碎米率(%)：").append(mRecordDaMi.totalBrokenRice).append('\r');
            //小碎米质量
            stringBuilder.append("小碎米质量(G)：").append(mRecordDaMi.smallBrokenRiceQuality).append('\r');
            //小碎米率
            stringBuilder.append("小碎米率(%)：").append(mRecordDaMi.smallBrokenRice).append('\r');
            //出糙率记录员
            stringBuilder.append("碎米记录员：").append(mRecordDaMi.brokenRiceRecorder).append('\r');
        }

        //黄粒米记录员
        if(!TextUtils.isEmpty(mRecordDaMi.yellowGrainRiceRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("黄粒米").append('\r');
            stringBuilder.append("\r");
            //谷外糙米质量
            stringBuilder.append("黄粒米质量(G)：").append(mRecordDaMi.yellowGrainRiceSampleQuality).append('\r');
            //谷外糙米质量
            stringBuilder.append("黄粒米质量(G)：").append(mRecordDaMi.yellowGrainRiceQuality).append('\r');
            //谷外糙米质量
            stringBuilder.append("黄粒米率(%)：").append(mRecordDaMi.yellowGrainRiceResult).append('\r');
            //黄粒米记录员
            stringBuilder.append("黄粒米记录员：").append(mRecordDaMi.yellowGrainRiceRecorder).append('\r');
        }
        //互混记录员
        if(!TextUtils.isEmpty(mRecordDaMi.mixedGrainRecorder)){
            stringBuilder.append("\r");
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            stringBuilder.append("互混").append('\r');
            stringBuilder.append("\r");
            //谷外糙米质量
            stringBuilder.append("互混质量(G)：").append(mRecordDaMi.massOfMixedSample).append('\r');
            //谷外糙米质量
            stringBuilder.append("异种粮质量(G)：").append(mRecordDaMi.mixedGrainQuality).append('\r');
            //谷外糙米质量
            stringBuilder.append("互混结果(%)：").append(mRecordDaMi.mixedGrainResult).append('\r');
            //黄粒米记录员
            stringBuilder.append("互混记录员：").append(mRecordDaMi.mixedGrainRecorder).append('\r');
        }

        var i = 0
        while (i < (1.0 - 2 / 16.0) * 16) {
            stringBuilder.append(" ")
            i++
        }
        InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
        InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
    }

    /**
     * 大米打印
     */
    fun DaMiPrinting(mRecordDaMiList: List<RecordDaMi>,mSubclass: String){
        for (mRecordDaMi in mRecordDaMiList){
            if(!mRecordDaMi.select){
                continue
            }
            val stringBuilder = StringBuilder()
            val title = "大米计算"
            val retract = ((1.0 - title.length / 16.0) * 16).toInt()
            for (i in 0 until retract) {
                stringBuilder.append(" ")
            }
            stringBuilder.append(title).append('\r')
            //编号
            stringBuilder.append("编号：").append(mRecordDaMi.uniqueId).append('\r')
            //时间
            stringBuilder.append("时间：").append(TimeUtils.millis2String(mRecordDaMi.inspectionDate)).append('\r')
            when (mSubclass) {
                "杂质" -> {
                    /**
                     * 杂质记录员
                     */
                    if(!TextUtils.isEmpty(mRecordDaMi.impurityRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("杂质").append('\r')
                        stringBuilder.append("\r")
                        // 大米-试样质量
                        stringBuilder.append("试样质量(G)：").append(mRecordDaMi.riceSampleQuality).append('\r');
                        //大米-杂质
                        stringBuilder.append("大米杂质(G)：").append(mRecordDaMi.riceImpurity).append('\r');
                        //杂质率
                        stringBuilder.append("杂质率(%)：").append(mRecordDaMi.impurityResults).append('\r');
                        //杂质记录员
                        stringBuilder.append("杂质记录员：").append(mRecordDaMi.impurityRecorder).append('\r');
                    }
                }
                "不完善" -> {
                    //不完善粒-试样质量
                    if(!TextUtils.isEmpty(mRecordDaMi.imperfectGrainRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("不完善粒").append('\r');
                        stringBuilder.append("\r");
                        // 不完善粒-试样质量
                        stringBuilder.append("试样质量(G)：").append(mRecordDaMi.qualityOfImperfectGrainSample).append('\r');
                        /**
                         * 细分
                         */
                        if(SPUtils.getInstance().getBoolean("ImperfectSubdivision",false)){
                            //未熟粒
                            stringBuilder.append("未熟粒(G)：").append(mRecordDaMi.immatureBrokenGrainQuality).append('\r')
                            //未熟粒率
                            stringBuilder.append("未熟粒率(%)：").append(mRecordDaMi.immatureGrainResult).append('\r')
                            //虫蚀粒
                            stringBuilder.append("虫蚀粒(G)：").append(mRecordDaMi.insectBoredQuality).append('\r')
                            //虫蚀率率
                            stringBuilder.append("虫蚀率率(%)：").append(mRecordDaMi.insectBoredResult).append('\r')
                            //病斑粒
                            stringBuilder.append("病斑粒(G)：").append(mRecordDaMi.mottledQuality).append('\r')
                            //病斑粒率
                            stringBuilder.append("病斑粒率(%)：").append(mRecordDaMi.mottledResult).append('\r')
                            //生霉粒
                            stringBuilder.append("生霉粒(G)：").append(mRecordDaMi.moldWeight).append('\r')
                            //生霉粒率
                            stringBuilder.append("生霉粒率(%)：").append(mRecordDaMi.moldResult).append('\r')
                            //糙米粒
                            stringBuilder.append("糙米粒(G)：").append(mRecordDaMi.incompleteBrownRiceQuality).append('\r')
                            //糙米粒率
                            stringBuilder.append("糙米粒率(%)：").append(mRecordDaMi.rougheningResults).append('\r')

                        }else{
                            //不完善粒质量
                            stringBuilder.append("不完善粒质量(G)：").append(mRecordDaMi.imperfectGrainQuality).append('\r');
                        }

                        //不完善粒-结果
                        stringBuilder.append("不完善粒率(%)：").append(mRecordDaMi.resultOfImperfectGrain).append('\r');
                        //出糙率记录员
                        stringBuilder.append("不完善粒记录员：").append(mRecordDaMi.imperfectGrainRecorder).append('\r');
                    }
                }
                "碎米率" -> {
                    //小碎米记录员
                    if(!TextUtils.isEmpty(mRecordDaMi.brokenRiceRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("碎米").append('\r');
                        stringBuilder.append("\r");
                        //碎米-试样质量
                        stringBuilder.append("碎米质量(G)：").append(mRecordDaMi.qualityOfBrokenRiceSample).append('\r');
                        //碎米质量
                        stringBuilder.append("碎米质量(G)：").append(mRecordDaMi.brokenRiceQuality).append('\r');
                        //碎米率
                        stringBuilder.append("碎米率(%)：").append(mRecordDaMi.totalBrokenRice).append('\r');
                        //小碎米质量
                        stringBuilder.append("小碎米质量(G)：").append(mRecordDaMi.smallBrokenRiceQuality).append('\r');
                        //小碎米率
                        stringBuilder.append("小碎米率(%)：").append(mRecordDaMi.smallBrokenRice).append('\r');
                        //出糙率记录员
                        stringBuilder.append("碎米记录员：").append(mRecordDaMi.brokenRiceRecorder).append('\r');
                    }
                }
                "黄粒米" -> {
                    //黄粒米记录员
                    if(!TextUtils.isEmpty(mRecordDaMi.yellowGrainRiceRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("黄粒米").append('\r');
                        stringBuilder.append("\r");
                        //谷外糙米质量
                        stringBuilder.append("黄粒米质量(G)：").append(mRecordDaMi.yellowGrainRiceSampleQuality).append('\r');
                        //谷外糙米质量
                        stringBuilder.append("黄粒米质量(G)：").append(mRecordDaMi.yellowGrainRiceQuality).append('\r');
                        //谷外糙米质量
                        stringBuilder.append("黄粒米率(%)：").append(mRecordDaMi.yellowGrainRiceResult).append('\r');
                        //黄粒米记录员
                        stringBuilder.append("黄粒米记录员：").append(mRecordDaMi.yellowGrainRiceRecorder).append('\r');
                    }
                }
                "互混" -> {
                    //互混记录员
                    if(!TextUtils.isEmpty(mRecordDaMi.mixedGrainRecorder)){
                        stringBuilder.append("\r");
                        var i = 0
                        while (i < (1.0 - 2 / 16.0) * 16) {
                            stringBuilder.append(" ")
                            i++
                        }
                        stringBuilder.append("互混").append('\r');
                        stringBuilder.append("\r");
                        //谷外糙米质量
                        stringBuilder.append("互混质量(G)：").append(mRecordDaMi.massOfMixedSample).append('\r');
                        //谷外糙米质量
                        stringBuilder.append("异种粮质量(G)：").append(mRecordDaMi.mixedGrainQuality).append('\r');
                        //谷外糙米质量
                        stringBuilder.append("互混结果(%)：").append(mRecordDaMi.mixedGrainResult).append('\r');
                        //黄粒米记录员
                        stringBuilder.append("互混记录员：").append(mRecordDaMi.mixedGrainRecorder).append('\r');
                    }
                }
            }
            var i = 0
            while (i < (1.0 - 2 / 16.0) * 16) {
                stringBuilder.append(" ")
                i++
            }
            InitializeUtils.getThermalService()?.usbSendDatas(stringBuilder.toString().toByteArray(Charset.forName("gbk")))
            InitializeUtils.getThermalService()?.usbSendDatas(BrightekCommandM.t1b4a(100))
        }
    }


}